package com.wxh.user.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.wxh.user.entity.User;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author wxh
 */
@RestController
public class UserController {

    @Autowired
    private EurekaClient eurekaClient;

    @GetMapping("/user/info")
    public String info() {
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("PROVIDER-USER", false);
        return instanceInfo.getHomePageUrl();
    }

    @GetMapping("/user/{userId}")
    public User getUser(@PathVariable Long userId) {
        User user = new User();
        user.setUserId(userId);
        user.setUserName("测试用户");
        user.setCreateDate(new Date());
        return user;
    }

    @RequestMapping(value = "/getuser", method = RequestMethod.POST)
    public User getUser(@RequestBody User user) {
        if (user != null) {
            if (user.getUserId() <= 0) {
                user.setUserId(RandomUtils.nextLong());
            }
            if (StringUtils.isEmpty(user.getUserName())) {
                user.setUserName("小李");
            }
            user.setCreateDate(new Date());
        }
        return user;
    }

}
